

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>命令行界面 &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="用户定义属性" href="attributes.html" />
    <link rel="prev" title="分布式优化" href="distributed.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../installation.html">安装</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">教程</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="first.html">第一个优化例子</a></li>
<li class="toctree-l2"><a class="reference internal" href="configurations.html">高级配置</a></li>
<li class="toctree-l2"><a class="reference internal" href="rdb.html">用 RDB 后端保存/恢复 Study</a></li>
<li class="toctree-l2"><a class="reference internal" href="distributed.html">分布式优化</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">命令行界面</a></li>
<li class="toctree-l2"><a class="reference internal" href="attributes.html">用户定义属性</a></li>
<li class="toctree-l2"><a class="reference internal" href="pruning.html">对无望的 Trial 进行剪枝 (Pruning)</a></li>
<li class="toctree-l2"><a class="reference internal" href="sampler.html">用户定义的采样器 (Sampler)</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
          <li><a href="index.html">教程</a> &raquo;</li>
        
      <li>命令行界面</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="../_sources/tutorial/cli.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="command-line-interface">
<span id="cli"></span><h1>命令行界面<a class="headerlink" href="#command-line-interface" title="永久链接至标题">¶</a></h1>
<table class="colwidths-given docutils align-default">
<colgroup>
<col style="width: 33%" />
<col style="width: 67%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>命令</p></th>
<th class="head"><p>功能描述</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>create-study</p></td>
<td><p>创建一个新的 study.</p></td>
</tr>
<tr class="row-odd"><td><p>delete-study</p></td>
<td><p>删除所给的 study.</p></td>
</tr>
<tr class="row-even"><td><p>dashboard</p></td>
<td><p>启动 web 面板 (beta).</p></td>
</tr>
<tr class="row-odd"><td><p>storage upgrade</p></td>
<td><p>升级数据库 schema.</p></td>
</tr>
<tr class="row-even"><td><p>studies</p></td>
<td><p>输出 study 列表</p></td>
</tr>
<tr class="row-odd"><td><p>study optimize</p></td>
<td><p>针对一个 study 开始优化过程。</p></td>
</tr>
<tr class="row-even"><td><p>study set-user-attr</p></td>
<td><p>设置 study 的用户属性。</p></td>
</tr>
</tbody>
</table>
<p>Optuna 提供的命令行界面包括上表中的所有命令。</p>
<p>如果你不使用 Ipython shell, 而是写 Python 脚本文件的话，编写像下面这样的脚本是完全可以的：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">optuna</span>


<span class="k">def</span> <span class="nf">objective</span><span class="p">(</span><span class="n">trial</span><span class="p">):</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_uniform</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">create_study</span><span class="p">()</span>
    <span class="n">study</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span><span class="n">objective</span><span class="p">,</span> <span class="n">n_trials</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Best value: </span><span class="si">{}</span><span class="s1"> (params: </span><span class="si">{}</span><span class="s1">)</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study</span><span class="o">.</span><span class="n">best_value</span><span class="p">,</span> <span class="n">study</span><span class="o">.</span><span class="n">best_params</span><span class="p">))</span>
</pre></div>
</div>
<p>然而，通过 <code class="docutils literal notranslate"><span class="pre">optuna</span></code> 命令，我们可以少写一些上面这样的模版代码。假设有一个 <code class="docutils literal notranslate"><span class="pre">foo.py</span></code> 文件，它只包含如下代码：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">objective</span><span class="p">(</span><span class="n">trial</span><span class="p">):</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_uniform</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
</pre></div>
</div>
<p>即使在这种情况下，我们也可以利用如下命令启动优化过程（请暂时忽略 <code class="docutils literal notranslate"><span class="pre">--storage</span> <span class="pre">sqlite:///example.db</span></code> 的作用, 关于它的具体描述见 <a class="reference internal" href="rdb.html#rdb"><span class="std std-ref">用 RDB 后端保存/恢复 Study</span></a>）。</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ cat foo.py
def objective<span class="o">(</span>trial<span class="o">)</span>:
    <span class="nv">x</span> <span class="o">=</span> trial.suggest_uniform<span class="o">(</span><span class="s1">&#39;x&#39;</span>, -10, <span class="m">10</span><span class="o">)</span>
    <span class="k">return</span> <span class="o">(</span>x - <span class="m">2</span><span class="o">)</span> ** <span class="m">2</span>

$ <span class="nv">STUDY_NAME</span><span class="o">=</span><span class="sb">`</span>optuna create-study --storage sqlite:///example.db<span class="sb">`</span>
$ optuna study optimize foo.py objective --n-trials<span class="o">=</span><span class="m">100</span> --storage sqlite:///example.db --study-name <span class="nv">$STUDY_NAME</span>
<span class="o">[</span>I <span class="m">2018</span>-05-09 <span class="m">10</span>:40:25,196<span class="o">]</span> Finished a trial resulted in value: <span class="m">54</span>.353767789264026. Current best value is <span class="m">54</span>.353767789264026 with parameters: <span class="o">{</span><span class="s1">&#39;x&#39;</span>: -5.372500782588228<span class="o">}</span>.
<span class="o">[</span>I <span class="m">2018</span>-05-09 <span class="m">10</span>:40:25,197<span class="o">]</span> Finished a trial resulted in value: <span class="m">15</span>.784266965526376. Current best value is <span class="m">15</span>.784266965526376 with parameters: <span class="o">{</span><span class="s1">&#39;x&#39;</span>: <span class="m">5</span>.972941852774387<span class="o">}</span>.
...
<span class="o">[</span>I <span class="m">2018</span>-05-09 <span class="m">10</span>:40:26,204<span class="o">]</span> Finished a trial resulted in value: <span class="m">14</span>.704254135013741. Current best value is <span class="m">2</span>.280758099793617e-06 with parameters: <span class="o">{</span><span class="s1">&#39;x&#39;</span>: <span class="m">1</span>.9984897821018828<span class="o">}</span>.
</pre></div>
</div>
<p>请注意，<code class="docutils literal notranslate"><span class="pre">foo.py</span></code> 中只包含目标函数的定义。通过向 <code class="docutils literal notranslate"><span class="pre">optuna</span> <span class="pre">study</span> <span class="pre">optimize</span></code> 命令传递该文件名和对应目标函数的方法名，我们就可以启动优化过程。</p>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="attributes.html" class="btn btn-neutral float-right" title="用户定义属性" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="distributed.html" class="btn btn-neutral float-left" title="分布式优化" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>